﻿//获取可视区域的宽高，不含border
function getClient(){
		return {
			width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0,
			height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0
		}

	}

//滚动
function getScroll(){
		return {
			left:window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0,
			top:window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0
		}

	}
// 封装鼠标滚轮监听
function MouseWheel(obj,fn){
    var ff = window.navigator.userAgent.indexOf("Firefox");
    // 判断火狐和非火狐
    if ( ff == -1) {
        _addEventListener(obj, "mousewheel", wheel)
    } else {
        _addEventListener(obj, "DOMMouseScroll", wheel)
    }
    // 滚轮滚动时的事件驱动程序
    function wheel(e) {
        e = e || window.event;
        // 声明页面向下
        var down = true;
        // 判断往哪个方向滑动
        if (e.wheelDelta) {
            down = e.wheelDelta > 0;
        } else {
            down = e.detail < 0;
        }
        
        fn && fn(down)
    }
}


// 封装添加事件监听
function _addEventListener(obj, type, callback) {
    if (window.addEventListener) {
        obj.addEventListener(type, callback)
    } else {
        obj == window ? document : obj;
        obj.attachEvent("on" + type, callback)
    }
}




//缓动动画
/*不透明度*/
	function animate(obj,json,fn){
		clearInterval(obj.timer)
		obj.timer=setInterval(function(){
			var flag = true; //声明一个阀值
			for(var k in json){
				var distance= json[k]
				if(k=='opacity'){
					var leader = getStyle(obj,k)*100
						distance = distance*100
					var step = (distance - leader)/10
					step = leader>distance? Math.floor(step):Math.ceil(step)
					leader = leader + step
					//设定每一个样式属性值
					obj.style[k] = leader/100
					console.log(obj.style[k])
					
				}else if(k=='zIndex'){
						var leader = distance;
						obj.style.zIndex=distance
				}else{
					//获取到每一个样式的当前位置
					var leader = parseInt(getStyle(obj,k))
					//json[k] 获取每一个属性的目标距离
					var step = (distance - leader)/10
					step = leader>distance? Math.floor(step):Math.ceil(step)
					
					leader = leader + step
					//设定每一个样式属性值
					obj.style[k] = leader +'px'
					
				}
				if(leader==distance){
						
				}else{
					flag = false 
				}
			}
			if(flag){
				clearInterval(obj.timer)
				fn && fn()
			}
		},16)
	}
	//竖向
	function animateT(obj,json,fn){
		clearInterval(obj.timer)
		obj.timer=setInterval(function(){
			var flag = true; //声明一个阀值
			for(var k in json){
				var distance= json[k]
				if(k=='opacity'){
					var leader = getStyle(obj,k)*100
						distance = distance*100
					var step = (distance - leader)/10
					step = leader>distance? Math.floor(step):Math.ceil(step)
					leader = leader + step
					//设定每一个样式属性值
					obj.style[k] = leader/100
					console.log(obj.style[k])
					
				}else if(k=='zIndex'){
						var leader = distance;
						obj.style.zIndex=distance
				}else{
					//获取到每一个样式的当前位置
					var leader = parseInt(getStyle(obj,k))
					//json[k] 获取每一个属性的目标距离
					var step = (distance - leader)/10
					step = leader>distance? Math.floor(step):Math.ceil(step)
					
					leader = leader + step
					//设定每一个样式属性值
					obj.style[k] = leader +'px'
					
				}
				if(leader==distance){
						
				}else{
					flag = false 
				}
			}
			if(flag){
				clearInterval(obj.timer)
				fn && fn()
			}
		},16)
	}
//获取样式属性	
function getStyle(obj,str){
		if(window.getComputedStyle){
			return window.getComputedStyle(obj,null)[str]
		}else{
			return  obj.currenStyle[str]//兼容性写法
		}
	}	
	
//小球碰撞     5 
function ball(obj,le,to){
	var leftnum=le,
		topnum=to
	var maxw=getClient().width-obj.offsetWidth,
		maxh=getClient().height-obj.offsetHeight
		
		
		setInterval(function(){
			
			var left=obj.offsetLeft+leftnum,
				top=obj.offsetTop+topnum
				
				if (top>=maxh) {
					top=maxh
					topnum=-topnum
					changeColor(obj)
				} else if(top<=0){
					top=0
					topnum=-topnum
					changeColor(obj)
				}
				if (left>=maxw) {
					left=maxw
					leftnum=-leftnum
					changeColor(obj)
				} else if(left<=0){
					left=0
					leftnum=-leftnum
					changeColor(obj)
				}
			/*for (var i=0;i<one.length;i++) {*/
					
				/*}	*/
			obj.style.left=left+'px'
			obj.style.top=top+'px'
			
			
		},16)
	}



	//随机背景颜色
	function changeColor(obj){
		var r=Math.floor(Math.random()*256)
		var g=Math.floor(Math.random()*256)
		var b=Math.floor(Math.random()*256)
		obj.style.backgroundColor="rgb("+r+","+g+","+b+")"
	}


	function ajax(method,url,data,callback){
		var xhr;
		try{
			xhr = new ActiveXObject('Microsoft.XMLHTTP')
		}catch(e){
			console.log(e) //参数 表示错误对象
			xhr = new XMLHttpRequest();
		}
		if(method=='get'){
			xhr.open('get',url+"?"+data)
			xhr.send(null)
		}else{
			xhr.open('post',url)
			xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded')
			xhr.send(data)
		}
		
		xhr.onreadystatechange =function(){
			if(this.readyState==4&&this.status==200){
				callback&&callback(this.responseText)
			}
		}
	}
